import cfg from '../../config';
import {getView} from '../../util/api';
import {formatDate, getCurrDay, randomColor, isArrContain, isArrNoContain, findStr, backPage} from '../../util/aid';
import Dialog from '../../lib/kdialog';
const dialog = new Dialog();

const $ = require('../../lib/kdom');
let _view = require('raw-loader!./pay-result.html');

// let _data = null;//父页面的数据
// let _opts = null;//父页面的参数
// let _currSaet = null;
let _order = null;//订单数据
let _result = null;

export default class {
  path = '/train/pay-result$';
  hash = '/train/pay-result';
  title = '支付结果';

  constructor(opt) {
    opt = opt || {};
    this.path = opt.path || this.path;
    this.hash = opt.hash || this.hash;
    this.title = opt.title || this.title;
  }

  // 输出视图
  view(cb) {
    if (!_view) {
      // 静态资源浏览器有缓存,增加时标,强制刷新!
      getView(`${cfg.train.view.payResult}?t=${(+new Date())}`, '', (rs) => {
        _view = rs;
        cb(null, _view);
      });
    } else
      cb(null, _view);
  }

  // 在已经加载的视图上操作
  bind(dv, params) {
    console.log(params);

    // _data = params.data;
    // _opts = params.opts;
    // _currSaet = params.currSaet;
    _order = params.order || {OrderAmount: "696", OrderNumber: "HC170629132845370471"};
    _result = params.result || {"successcode": "T", "info": "请求成功"};

    dialog.loading();
    // 加载支付结果
    loadPayResult();

    // backPage
    backPage('#pg-train-pay-result', 'backPage', () => {
      $.router.go('/train/order', '', true);
    });
  }

  /*bind*/
}

function loadPayResult() {
  // console.log(_result);
  let rc = '';
  if (_result && _result.successcode === 'T') {
    rc = payResultHtmlFun('<i class="weui_icon_success pay-result"></i><p>支付成功</p>', '<span class="button button-success" id="orderComplate">完成</span>');
    $.html(payResultBox, rc);
    // 完成
    orderComplate.onclick = () => {
      $.router.go('/train/order', '', true);
    };
  } else {
    rc = payResultHtmlFun('<i class="weui_icon_warn pay-result"></i><p>支付失败</p>', '<span class="button button-danger" id="orderFailure">返回</span>');
    $.html(payResultBox, rc);
    // 失败
    orderFailure.onclick = () => {
      $.router.go('/train/order', '', true);
    };
  }
  dialog.close();
}

function payResultHtmlFun(txt1, txt2) {
  return `<br>
    <div class="content-padded text-center">${txt1}</div>
    <br>
    <div class="content-padded" style="width: 80%;margin: auto;">
        <div>订单号：${_order.OrderNumber}</div>
        <div>实付金额：<span class="text-danger">￥${_order.OrderAmount}</span></div>
    </div>
    <br>
    <br>
    <div class="content-padded">
        <div class="button-item">${txt2}</div>
    </div>`;
}